package com.powernode.utils;

import com.powernode.common.Constants;
import com.powernode.domain.User;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 内容:
 * 获取 request
 * 获取 response
 * 获取 session
 * 获取 当前用户
 * 获取 当前用户名
 *
 * 作者: 动力节点 武汉
 */
public class WebMvcUtils {
    /**
     * 得到request
     */
    public static HttpServletRequest getRequest(){
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return servletRequestAttributes.getRequest();
    }

    /**
     * 得到response
     */
    public static HttpServletResponse getResponse(){
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return servletRequestAttributes.getResponse();
    }

    /**
     * 得到session
     */
    public static HttpSession getSession(){
        return getRequest().getSession();
    }
    /**
     * 得到application
     */
    public static ServletContext getContext(){
        return getRequest().getServletContext();
    }
    /**
     * 得到当前用户
     */
    public static User getCurrentUser(){
       User user= (User) getSession().getAttribute(Constants.SESSION_USER_KEY);
       return user;
    }

    /**
     * 得到当前用户名
     */
    public static String getCurrentUserName(){
        return getCurrentUser().getRealname();
    }



}
